$(document).ready(function() {
'use strict';
var translate = navigator.mozL10n.get;
var searchstring = "";
var activepage = "home";
var searchreq = null;
var searchscope = "general";
var searchpage = 1;
var scrollup = false;
$.ajaxSetup({xhr:function() {return new XMLHttpRequest({mozSystem:true})}});
function showError(text) {
$("#error").text(text);
$("meta[name='theme-color']").attr("content","#323232");
setTimeout(function() {
$("#error_holder").slideDown();
},500);
setTimeout(function() {
$("#error_holder").slideUp(function() {
$("meta[name='theme-color']").attr("content","#3faa7e");
});
},5000);
}
$("#searchform").submit(function(e) {
e.preventDefault();
var term = $("#term").val();
if(term.length > 0) {
$("#term").blur();
$("#searchform").hide();
$(".searx_logo").hide();
$("#autocomplete").hide();
$("#softkey-center").text("");
$("#softkey-left").text("");
$("#results").hide();
$("#no_results").hide();
$(".loading").show();
$("#pagetitle").text($("#term").val()+" - Searx");
activepage = "loading";
if(searchreq && searchreq.readyState != 4) searchreq.abort();
searchreq = $.ajax('https://'+srxsettings.instances.find(function(data) {return data.active}).url+'/search?format=json&safesearch='+srxsettings.safesearch+'&language='+srxsettings.language+'&categories='+searchscope+'&engines='+srxsettings.engines[searchscope].join(',')+'&pageno='+searchpage+'&q='+encodeURIComponent(term)).done(function(response) {
if(response.length == 0) {
showError(translate('searching_error'));
return;
}
var searchres = response.results;
if(searchres.length === 0) {
if(searchpage == 1) {
$(".loading").hide();
$("#no_results").show();
}
else {
$(".loading").hide();
showError(translate("no_more_pages_available"));
$("#results").show();
}
}
else {
activepage = "results";
$("#result_list").empty();
searchres.forEach(function(doc,i) {
if(searchscope == "images") {
if(doc.thumbnail_src.substr(0,2) == "//") doc.thumbnail_src = "https:"+doc.thumbnail_src;
var result = $("<li>").addClass("navigable").addClass("image_result")
.append($("<div>").addClass("kui-list-cont").css("background-image","url("+doc.thumbnail_src+")"))
.keyup(function(e) {
if(e.keyCode == 0 && e.originalEvent.key == "SoftLeft") {
$("#results").hide();
$(".loading").show();
if(doc.img_src.substr(0,2) == "//") doc.img_src = "https:"+doc.img_src;
var xhr = new XMLHttpRequest({mozSystem:true});
xhr.open('GET',doc.img_src,true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
if(this.status == 200) {
var blob = this.response;
var dataurl = window.URL.createObjectURL(blob);
var random = Math.round(Math.random()*100000);
$(document.body).append($("<a>").attr("id",random).attr("download","").attr("href",dataurl));
$("#"+random)[0].click();
$("#"+random).remove();
$(".loading").hide();
$("#results").show();
}
};
xhr.send();
}
});
}
else {
var result = $("<li>").addClass("navigable")
.append($("<div>").addClass("kui-list-cont")
.append($("<p>").addClass("kui-pri").text(doc.title))
.append($("<p>").addClass("kui-thi").addClass("result_url").text(doc.url))
.append($("<p>").addClass("kui-sec").text(doc.content)));
}
result.keyup(function(e) {
if(e.keyCode == 13) {
$("meta[name='theme-color']").attr("content","#320374");
window.open(doc.url,"overlay");
}
});
result.keydown(function(e) {
if(searchpage > 1 && i == 0 && e.keyCode == 38) {
searchpage--;
scrollup = true;
$("#searchform").submit();
}
else if(i != 0 && e.keyCode == 38) $("#result_list li").eq(i-1).focus();
else if(i+1 == searchres.length && e.keyCode == 40) {
searchpage++;
$("#searchform").submit();
}
else if(e.keyCode == 40) $("#result_list li").eq(i+1).focus();
});
$("#result_list").append(result);
});
$("#searchpage").text(searchpage);
$(".loading").hide();
$("#results").show();
$("#softkey-center").text(translate("visit"));
if(searchscope == "images") $("#softkey-left").text(translate("download"));
else $("#softkey-left").text("");
naviBoard.destroyCurrentNavigationView();
naviBoard.setNavigation("result_list");
setTimeout(function() {
if(scrollup) $("#result_list li").eq(searchres.length-1).focus();
else $("#result_list li").eq(0).focus();
scrollup = false;
},0);
}
}).fail(function(data,status,error) {
if(!error) {
error = translate('searching_error');
}
if(error != "abort") showError(error);
});
}
});
$("#term").focus();
$("#term").on("input",function(e) {
if($(this).val().length > 0 && srxsettings.autocomplete != "off") {
activepage = "typing";
$(".searx_logo").hide();
if($("#term").val() != searchstring) {
searchstring = $("#term").val();
$.ajax("https://"+srxsettings.instances.find(function(data) {return data.active}).url+"/autocompleter?autocomplete="+srxsettings.autocomplete+"&language="+srxsettings.language+"&q="+encodeURIComponent($("#term").val())).done(function(data) {
if(activepage == "typing") {
$("#autocomplete_list").empty();
if(data.length > 0) {
data.forEach(function(doc) {
if(doc.length > 0) {
$("#autocomplete_list").append($("<li>").addClass("navigable")
.append($("<div>").addClass("kui-list-cont")
.append($("<p>").addClass("kui-pri").text(doc)))
.keyup(function(e) {
if(e.keyCode == 13) {
$("#term").val(doc);
$("#searchform").submit();
}
}));
}
});
naviBoard.destroyCurrentNavigationView();
naviBoard.setNavigation("autocomplete_nav");
$("#autocomplete").show();
}
else {
naviBoard.destroyCurrentNavigationView();
$("#term").focus();
$("#autocomplete").hide();
}
}
});
}
}
else {
activepage = "home";
naviBoard.destroyCurrentNavigationView();
$("#term").focus();
$("#autocomplete").hide();
$(".searx_logo").show();
}
});
$("#settings .navigable").eq(0).focus(function() {
$("#settings").scrollTop(0);
});
$(document).keydown(function(e) {
if(e.keyCode == 38 || e.keyCode == 40) {
e.preventDefault();
}
if(e.keyCode == 8 && activepage == "settings_option") {
e.preventDefault();
activepage = "settings";
$("#settings_option").hide();
$("#softkey-left").text("");
$("#softkey-right").text("");
$("#cur_instance").text(srxsettings.instances.find(function(data) {return data.active}).name);
$("#cur_language").text(srxdata.language.find(function(data) {if(data.value == this) return true},srxsettings.language).name);
$("#cur_autocomplete").text(srxdata.autocomplete.find(function(data) {if(data.value == this) return true},srxsettings.autocomplete).name);
$("#cur_safesearch").text(srxdata.safesearch.find(function(data) {if(data.value == this) return true},srxsettings.safesearch).name);
$(".set_engines").each(function() {
var set_scope = $(this).data("scope");
var engine_list = new Array;
for(var i=0;i<srxsettings.engines[set_scope].length;i++) {
engine_list.push(srxdata.engines[set_scope].find(function(data) {if(data.value == this) return true},srxsettings.engines[set_scope][i]).name);
}
$(this).find(".cur_engines").text(engine_list.join(", "));
});
$("#settings").show();
naviBoard.destroyCurrentNavigationView();
naviBoard.setNavigation("settings");
}
else if(e.keyCode == 8 && activepage == "settings_instance") {
e.preventDefault();
$("#settings_instance").hide();
$("#set_instance").trigger($.Event("keyup",{keyCode:13}));
}
else if(e.keyCode == 8 && activepage == "loading_test") {
e.preventDefault();
if(searchreq && searchreq.readyState != 4) searchreq.abort();
$(".loading").hide();
$("#softkey-center").text(translate("save"));
$("#settings_instance").show();
}
else if(e.keyCode == 8 && activepage != "home") {
e.preventDefault();
if(searchreq && searchreq.readyState != 4) searchreq.abort();
$("#results").hide();
$("#autocomplete").hide();
$(".loading").hide();
$("#settings").hide();
$("#no_results").hide();
$("#term").val("");
$("#softkey-center").text(translate("search"));
$("#softkey-left").text("");
$("#searchform").show();
$(".searx_logo").show();
$("#tabs").show();
$("#softkey-right").text(translate("settings"));
$("#pagetitle").text("Searx");
$("#term").focus();
naviBoard.destroyCurrentNavigationView();
searchpage = 1;
activepage = "home";
}
if((e.keyCode == 37 || e.keyCode == 39) && (!$("#term").is(":focus") || $("#term").val().length == 0) && $("#tabs").is(":visible")) {
if(e.keyCode == 37) {
if($("#tabs li").index($("#tabs li.active")) == 0) var newid = $("#tabs li").length-1;
else var newid = $("#tabs li").index($("#tabs li.active"))-1;
}
else {
if($("#tabs li").index($("#tabs li.active")) == $("#tabs li").length-1) var newid = 0;
else var newid = $("#tabs li").index($("#tabs li.active"))+1;
}
var tabwidth = 0;
for(var i=0;i<newid;i++) {
tabwidth = tabwidth+$("#tabs li").eq(i).outerWidth()+4;
}
tabwidth = tabwidth-(($(window).width()-$("#tabs li").eq(newid).width())/2)+10;
searchscope = $("#tabs li").eq(newid).data("scope");
$("#tabs li.active").removeClass("active");
$("#tabs").scrollLeft(tabwidth);
$("#tabs li").eq(newid).addClass("active");
if(activepage == "results" || activepage == "loading") {
searchpage = 1;
$("#searchform").submit();
}
}
if(activepage != "settings" && activepage != "settings_option" && activepage != "settings_instance" && activepage != "loading_test" && e.keyCode == 0 && e.originalEvent.key == "SoftRight") {
activepage = "settings";
$("#tabs").hide();
$(".searx_logo").hide();
$("#searchform").hide()
$("#autocomplete").hide();
$("#results").hide();
$(".loading").hide();
$("#no_results").hide();
$("#softkey-right").text("");
$("#softkey-left").text("");
$("#softkey-center").text(translate("select"));
$("#pagetitle").text(translate("settings")+" - Searx");
$("#cur_instance").text(srxsettings.instances.find(function(data) {return data.active}).name);
$("#cur_language").text(srxdata.language.find(function(data) {if(data.value == this) return true},srxsettings.language).name);
$("#cur_autocomplete").text(srxdata.autocomplete.find(function(data) {if(data.value == this) return true},srxsettings.autocomplete).name);
$("#cur_safesearch").text(srxdata.safesearch.find(function(data) {if(data.value == this) return true},srxsettings.safesearch).name);
$(".set_engines").each(function() {
var set_scope = $(this).data("scope");
var engine_list = new Array;
for(var i=0;i<srxsettings.engines[set_scope].length;i++) {
engine_list.push(srxdata.engines[set_scope].find(function(data) {if(data.value == this) return true},srxsettings.engines[set_scope][i]).name);
}
$(this).find(".cur_engines").text(engine_list.join(", "));
});
$("#settings").show();
naviBoard.destroyCurrentNavigationView();
naviBoard.setNavigation("settings");
}
});
$("#set_instance").keyup(function(e) {
if(e.keyCode == 13) {
activepage = "settings_option";
$("#settings").hide();
$("#settings_option_list").empty();
$("#settings_option_header").text(translate("searx_instance"));
for(var i=0;i<srxsettings.instances.length;i++) {
(function(index) {
$("#settings_option_list").append($("<li>").addClass("navigable").addClass("instance_sel")
.append($("<div>").addClass("kui-list-cont")
.append($("<p>").addClass("kui-pri")
.append($("<input>").addClass("kui-radio").attr("type","radio").attr("id","instance_"+srxsettings.instances[i].url.replace(/./g,"_")))
.append($("<label>").attr("for","instance_"+srxsettings.instances[i].url.replace(/./g,"_")).text(srxsettings.instances[i].name)))
.append($("<p>").addClass("kui-sec").text(srxsettings.instances[i].url))).keyup(function(e) {
if(e.keyCode == 13) {
for(var x=0;x<srxsettings.instances.length;x++) {
srxsettings.instances[x].active = false;
}
srxsettings.instances[index].active = true;
localStorage.setItem("instances",JSON.stringify(srxsettings.instances));
$("#settings_option_list input").removeAttr("checked");
$("#instance_"+srxsettings.instances[index].url.replace(/./g,"_")).attr("checked","checked");
}
else if(e.keyCode == 0 && e.originalEvent.key == "SoftLeft") {
activepage = "settings_instance";
$("#settings_option").hide();
$("#softkey-left").text("");
$("#softkey-right").text("");
$("#softkey-center").text(translate("save"));
$("#settings_instance").show();
naviBoard.destroyCurrentNavigationView();
naviBoard.setNavigation("settings_instance");
}
else if(e.keyCode == 0 && e.originalEvent.key == "SoftRight") {
if(srxsettings.instances[index].active == true) showError(translate("you_can_not_delete_the_searx_instance_you_are_currently_using"));
else {
srxsettings.instances.splice(index,1);
localStorage.instances = JSON.stringify(srxsettings.instances);
$("#set_instance").trigger($.Event("keyup",{keyCode:13}));
}
}
}));
if(srxsettings.instances[i].active == true) $("#instance_"+srxsettings.instances[i].url.replace(/./g,"_")).attr("checked","checked");
})(i);
}
$("#settings_option").show();
$("#softkey-left").text(translate("add"));
$("#softkey-right").text(translate("delete"));
naviBoard.destroyCurrentNavigationView();
naviBoard.setNavigation("settings_option_list");
}
});
$("#set_language").keyup(function(e) {
if(e.keyCode == 13) {
settings_radio("language","search_language");
}
});
$("#set_autocomplete").keyup(function(e) {
if(e.keyCode == 13) {
settings_radio("autocomplete","autocomplete");
}
});
$("#set_safesearch").keyup(function(e) {
if(e.keyCode == 13) {
settings_radio("safesearch","safe_search");
}
});
$(".set_engines").keyup(function(e) {
if(e.keyCode == 13) {
activepage = "settings_option";
var set_scope = $(this).data("scope");
$("#settings").hide();
$("#settings_option_list").empty();
$("#settings_option_header").text(translate("search_engines")+": "+$(this).find(".kui-pri").text());
for(var i=0;i<srxdata.engines[set_scope].length;i++) {
(function(index) {
$("#settings_option_list").append($("<li>").addClass("navigable").addClass("large")
.append($("<div>").addClass("kui-list-cont")
.append($("<p>").addClass("kui-pri")
.append($("<input>").addClass("kui-checkbox").attr("type","checkbox").attr("id","engines_"+srxdata.engines[set_scope][i].value.replace(/\+/g,"_")))
.append($("<label>").attr("for","engines_"+srxdata.engines[set_scope][i].value.replace(/\+/g,"_")).text(srxdata.engines[set_scope][i].name)))).keyup(function(e) {
if(e.keyCode == 13) {
var set_engine = srxdata.engines[set_scope][index].value;
if(srxsettings.engines[set_scope].indexOf(set_engine) == -1) {
srxsettings.engines[set_scope].push(set_engine);
$("#engines_"+set_engine.replace(/\+/g,"_")).attr("checked","checked");
}
else {
srxsettings.engines[set_scope].splice(srxsettings.engines[set_scope].indexOf(set_engine),1);
$("#engines_"+set_engine.replace(/\+/g,"_")).removeAttr("checked");
}
localStorage.setItem("engines_"+set_scope,JSON.stringify(srxsettings.engines[set_scope]));
}
}));
if(srxsettings.engines[set_scope].indexOf(srxdata.engines[set_scope][i].value) != -1) $("#engines_"+srxdata.engines[set_scope][i].value.replace(/\+/g,"_")).attr("checked","checked");
})(i);
}
$("#settings_option").show();
naviBoard.destroyCurrentNavigationView();
naviBoard.setNavigation("settings_option_list");
}
});
$("#set_about_searx").keyup(function(e) {
if(e.keyCode == 13) {
$("meta[name='theme-color']").attr("content","#320374");
window.open("https://github.com/asciimoo/searx/blob/master/README.rst","overlay");
}
});
$("#set_about_app").keyup(function(e) {
if(e.keyCode == 13) {
$("meta[name='theme-color']").attr("content","#320374");
window.open("https://notabug.org/kaitools/searx/src/master/README.md","overlay");
}
});
$("#set_license").keyup(function(e) {
if(e.keyCode == 13) {
$("meta[name='theme-color']").attr("content","#320374");
window.open("https://notabug.org/kaitools/searx/src/master/LICENSE","overlay");
}
});
$(document).focus(function() {
$("meta[name='theme-color']").attr("content","#3faa7e");
});
$("#settings_instance .kui-input").focus(function(e) {
$(e.target).closest(".kui-input-holder").addClass("kui-focus-within");
});
$("#settings_instance .kui-input").blur(function(e) {
$(e.target).closest(".kui-input-holder").removeClass("kui-focus-within");
});
$("#settings_instance").keyup(function(e) {
if(e.keyCode == 13) {
if($("#instance_name").val().length > 0 && $("#instance_url").val().length > 0) {
activepage = "loading_test";
$("#settings_instance").hide();
$("#softkey-center").text("");
$(".loading").show();
searchreq = $.ajax("https://"+$("#instance_url").val()+"/search?format=json&q=test").done(function(data) {
$(".loading").hide();
if(data.length == 0 || !data.results || data.results.length == 0) {
$("#softkey-center").text(translate("save"));
$("#settings_instance").show();
showError(translate("this_instance_does_not_work"));
}
else {
var add_instance = new Object();
add_instance.name = $("#instance_name").val();
add_instance.url = $("#instance_url").val();
add_instance.active = false;
srxsettings.instances.push(add_instance);
localStorage.instances = JSON.stringify(srxsettings.instances);
$("#instance_name").val("");
$("#instance_url").val("");
$("#set_instance").trigger($.Event("keyup",{keyCode:13}));
}
}).fail(function() {
$(".loading").hide();
$("#softkey-center").text(translate("save"));
$("#settings_instance").show();
showError(translate("this_instance_does_not_work"));
});
}
}
});
function settings_radio(option,title) {
activepage = "settings_option";
$("#settings").hide();
$("#settings_option_list").empty();
$("#settings_option_header").text(translate(title));
for(var i=0;i<srxdata[option].length;i++) {
(function(index) {
$("#settings_option_list").append($("<li>").addClass("navigable").addClass("large")
.append($("<div>").addClass("kui-list-cont")
.append($("<p>").addClass("kui-pri")
.append($("<input>").addClass("kui-radio").attr("type","radio").attr("id",option+"_"+srxdata[option][i].value))
.append($("<label>").attr("for",option+"_"+srxdata[option][i].value).text(srxdata[option][i].name)))).keyup(function(e) {
if(e.keyCode == 13) {
srxsettings[option] = srxdata[option][index].value;
localStorage.setItem(option,srxdata[option][index].value);
$("#settings_option_list input").removeAttr("checked");
$("#"+option+"_"+srxdata[option][index].value).attr("checked","checked");
}
}));
})(i);
}
$("#"+option+"_"+srxsettings[option]).attr("checked","checked");
$("#settings_option").show();
naviBoard.destroyCurrentNavigationView();
naviBoard.setNavigation("settings_option_list");
}
});